package domain;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import dal.ConnectionManager;

public class Material {

	/**Every material has its own id*/
	private Integer id;
	/**Name of the material*/
	private String name;
	/**ResourceId of the material*/
	private Integer resourceId;
	ConnectionManager mgr = ConnectionManager.getInstance();
	Connection con = mgr.getConnection();
	Statement st;
	
	/**
	 * Empty constructor of Material
	 */
	public Material() {
		super();
	}

	/**
	 * Constructor of Material
	 * @param name Name of the material
	 * @param resourceId resourceId of the material
	 */
	public Material(String name, Integer resourceId) {
		super();
		this.name = name;
		this.resourceId = resourceId;
	}
	
	/**
	 * Add new material
	 * @param myPerson Object of person
	 */
	public void addMaterial(Material myMaterial)

	{
		
		try{
			Statement st = con.createStatement();
		    st.executeUpdate("insert into material(id,name,resourceId) values ('"+myMaterial.getId()+"', '"+myMaterial.getName()+"','"+myMaterial.getResourceId()+"'");
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} 
	
	/**
	 * Delete material from material table
	 * @param myMaterial Object of material
	 */
	public void deleteMaterial(Material myMaterial){
		
		try{
			Statement st = con.createStatement();
			st.executeUpdate("Delete from material where id="+myMaterial.getId());
		} catch(SQLException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Update 
	 * @param myPerson Object of material
	 */
	
	public void updateMaterial(Material myMaterial){
		try{
			Statement st = con.createStatement();
			st.executeUpdate("Update material set name='"+myMaterial.getName()+"', resourceId='"+myMaterial.getResourceId()+"' ");
		} catch(SQLException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * Getters and Setters
	 * @return
	 */
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getResourceId() {
		return resourceId;
	}

	public void setResourceId(Integer resourceId) {
		this.resourceId = resourceId;
	}
	
	public void assignMaterialToResource(Material myMaterial,int resourceId)
	{
		try {
			st.executeUpdate("update material set resourceId="+resourceId+" where id="+myMaterial.getId()+"");
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<Material> selectbyResource(Resource myResource) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select m.id,m.name,m.resourceId "+
											"from material m "+
											"join resource r on m.resourceId=r.id "+
											"where r.id="+myResource.getResourceId()+";");
			ArrayList<Material> myMaterialList = new ArrayList<Material>();	
			if (rs.next()) {
			     do {
			    	 Material myMaterial = new Material();
			    	 myMaterial.setId(rs.getInt(1));
			    	 myMaterial.setName(rs.getString(2));
			    	 myMaterial.setResourceId(rs.getInt(3));
			    	 myMaterialList.add(myMaterial);
			       } while (rs.next());
			 	return myMaterialList;  
			     }
			  else {
			    return null;
		}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
